--
-- Author: yawei.zhu
-- Date: 2018-09-26 15:42:30
--
-- SummonBossController
--
local SummonBossView = import("..views.SummonBossView")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ControllerBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._model = app:getInst("BossModel")
end

function ClassRef:removeListeners()
    self.super.removeListeners(self)
end

function ClassRef:onEvent(eventName, data)
end

function ClassRef:listEvent()
    return {}
end


function ClassRef:openView(data)
    if not data then return end
    self._itemData = data
    local id = data[2] or 30301
    local tickInfo = GD:getWildBossTicketInfo(id)
    if not tickInfo then return end

    if cc.isDead(self._view) then
        self._view = SummonBossView.new(app.mvc.VIEWTYPE_BLUR)
        self._view:onCloseView(handler(self, self.closeView))
        self._view:onClearView(handler(self, self.clearView))
        self._view:onOperate(handler(self, self.onOperate))
        -- self._view:hide()
        self:addChild(self._view)
    end

    self._view:show()
    self._view:updateView(tickInfo)
end
function ClassRef:closeView(data)
    if self._view then
        self:removeChild(self._view, data)
        self:clearView()
    end
end
function ClassRef:clearView()
    if self._view then
        self._view = nil
    end
end

-- 打开宝箱
function ClassRef:openBoxView(item)
    local associateId = item:getAssociateId()
    if not associateId then return end

    local associateInfo = GD:queryItemById(associateId)
    if not associateInfo then return end

    local function showView(msg)
        if not msg then return end

        local data = {}
        local strs = {}
        local dayTimesLimit = tonumber(associateInfo.dayTimesLimit)
        local weekTimesLimit = tonumber(associateInfo.weekTimesLimit)
        local dayLeftTimes = msg.dayUseTimes and (dayTimesLimit-tonumber(msg.dayUseTimes)) or dayTimesLimit
        local weekLeftTimes = msg.weekUseTimes and (weekTimesLimit-tonumber(msg.weekUseTimes)) or weekTimesLimit
        
        strs[2] = string.format(L("lua_code_text_225"), weekLeftTimes, weekTimesLimit)
        if dayLeftTimes > weekLeftTimes then -- 如果每日剩余可开启次数大于每周剩余可开启次数，就重置每日可开启次数
            dayLeftTimes = weekLeftTimes
        end
        strs[1] = string.format(L("lua_code_text_176"), dayLeftTimes, dayTimesLimit)

        data.strs = strs
        data.boxType = Enums.BoxType.associate
        data.itemUid = item:getUid()
        data.itemId = item:getId()
        data.name = L(item:getName())

        app:sendMsg("BoxController", "openView", data)
    end

    -- 拉取最新次数
    self._model:reqItemUseTimes(item:getId(), showView)
end

-------------------------------------------------------


-------------------------------------------------------
function ClassRef:onOperate(op, data)
    print("SummonBossController:onOperate op = " .. op)
    if op == "onTrackWildBoss" then
        EM:notify("onTrackingWay",{trackNpcId={mapId=data[1], x=data[2]}, autoStartEvents=false, isTracking=true})
        FieldManager:setTrackData(Enums.trackType.wildBoss, self._itemData)
    elseif op == "reqSummonBoss" then
        -- self._model:reqSummonBoss(self._itemData[1], self._itemData[2])
        app:sendMsg("TeamMatchController", "startWildBoss", self._itemData)
    elseif op == "gotoTeam" then
        local params = {}
        params.id = Constant.FUNC_TYPE_TEAMMATCHING
        params.mode = "open"
        params.data = {targetId=Enums.TeamTarget.wildBoss}
        UIHandler:handleMessage(params)
    end
end

return ClassRef
